package com.haoshui.runner;


import com.haoshui.constants.RedisCacheConstans;
import com.haoshui.domain.entity.Article;
import com.haoshui.mapper.ArticleMapper;
import com.haoshui.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class ViewCountRuner implements CommandLineRunner {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
        //从数据库查询viewCount
        List<Article> articles = articleMapper.selectList(null);

        //存入redis
        Map<String, Integer> collect = articles.stream().collect(Collectors.toMap(article -> article.getId().toString(), article -> article.getViewCount().intValue()));
        redisCache.setCacheMap(RedisCacheConstans.VIEW_COUNT_KEY_IN_REDIS,collect );


    }
}
